// BASE SETUP

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var todoApi = require('./todoApi.js');
var appApiV1 = require('./appApiV1.js');


//configure app to use bodyParser()
//this will let us get the data from a POST

app.use(bodyParser.urlencoded({ extended: true }));

app.use(bodyParser.json());


var port = process.env.PORT || 3000;

//ROUTES FOR OUR API

// var router = express.Router();

// test route to make sure everything is working (accessed at GET http://localhost:3002/api)

/*router.get('/',function(req, res){
	res.json({ message: 'welcome to our api!'});
});*/


// more routes for our API will happen here
 
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api

// 所有的请求都走api/
app.use('/api', todoApi);
app.use('/appApi/v1', appApiV1);

//设置跨域访问
app.all('*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

// START THE SERVER
// =============================================================================
app.listen(port);

console.log('Magic happens on port ' + port);
